package com.xdw.addressbookbyjava.dao.impl;

import com.xdw.addressbookbyjava.MyApplication;
import com.xdw.addressbookbyjava.dao.ContactsDao;
import com.xdw.addressbookbyjava.model.Contacts;
import ohos.data.rdb.RdbPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 夏德旺 on 2021/1/5 21:18
 */
public class ContactsDaoImpl implements ContactsDao {
    public static final String TAG = "ContactsDaoImpl";
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, TAG);

    @Override
    public long insertContacts(Contacts contacts) {
        ValuesBucket values = new ValuesBucket();
        values.putString("name", contacts.getName());
        values.putInteger("gender", contacts.getGender());
        values.putString("phone", contacts.getPhone());
        long id = MyApplication.getRdbStore().insert("contacts", values);
        return id;
    }

    @Override
    public long deleteContacts(int id) {
        RdbPredicates rdbPredicates = new RdbPredicates("contacts").equalTo("id", id);
        HiLog.error(LABEL,"deleteid="+id);
        int num = MyApplication.getRdbStore().delete(rdbPredicates);
        return num;
    }

    @Override
    public List<Contacts> queryContactsById(int id) {
        List<Contacts> contactsList = new ArrayList<>();
        String[] columns = new String[]{"id", "name", "gender", "phone"};
        RdbPredicates rdbPredicates = new RdbPredicates("contacts").equalTo("id", id).orderByAsc("id");
        ResultSet resultSet = MyApplication.getRdbStore().query(rdbPredicates, columns);
        while (resultSet.goToNextRow()) {
            Contacts contacts = new Contacts();
            HiLog.error(LABEL, "id="+resultSet.getString(0));
            contacts.setId(resultSet.getInt(0));
            contacts.setName(resultSet.getString(1));
            contacts.setGender(resultSet.getInt(2));
            contacts.setPhone(resultSet.getString(3));
            contactsList.add(contacts);
        }
        return contactsList;
    }

    @Override
    public long updateContacts(Contacts contacts) {
        return 0;
    }

    @Override
    public List<Contacts> getAllContacts() {
        List<Contacts> contactsList = new ArrayList<>();
        String[] columns = new String[]{"id", "name", "gender", "phone"};
        RdbPredicates rdbPredicates = new RdbPredicates("contacts").orderByAsc("id");
        ResultSet resultSet = MyApplication.getRdbStore().query(rdbPredicates, columns);
        while (resultSet.goToNextRow()) {
            Contacts contacts = new Contacts();
            HiLog.error(LABEL, "id="+resultSet.getString(0));
            contacts.setId(resultSet.getInt(0));
            contacts.setName(resultSet.getString(1));
            contacts.setGender(resultSet.getInt(2));
            contacts.setPhone(resultSet.getString(3));
            contactsList.add(contacts);
        }
        return contactsList;
    }
}
